import sys
sys.path.append(r'Module/IndustryModel')
from CamOperation_class import CameraOperation
from MvCameraControl_class import *
from MvErrorDefine_const import *
from CameraParams_header import *

# 获取选取设备信息的索引，通过[]之间的字符去解析
def TxtWrapBy(start_str, end, all):
    start = all.find(start_str)
    if start >= 0:
        start += len(start_str)
        end = all.find(end, start)
        if end >= 0:
            return all[start:end].strip()

# 将返回的错误码转换为十六进制显示
def ToHexStr(num):
    chaDic = {10: 'a', 11: 'b', 12: 'c', 13: 'd', 14: 'e', 15: 'f'}
    hexStr = ""
    if (int)(num) < 0:
        num = num + 2 ** 32
    while num >= 16:
        digit = num % 16
        hexStr = chaDic.get(digit, str(digit)) + hexStr
        num //= 16
    hexStr = chaDic.get(num, str(num)) + hexStr
    return hexStr


def init_CameraDll():
    return loadCameraDll()

class CameraSDK:
    def __init__(self, n_connect_num=0, frame_rate=0, exposure_time=0, gain=0):
        self.obj_cam = None
        self.st_device_list = None
        self.obj_cam_operation = None
        self.isOpen = False
        self.isGrabbing = False
        self.isCamMode = False
        self.isFindCam = False
        self.n_connect_num = n_connect_num
        self.frame_rate = frame_rate
        self.exposure_time = exposure_time
        self.gain = gain

    def find_device(self):
        self.isFindCam = False
        self.st_device_list = MV_CC_DEVICE_INFO_LIST()
        ret = MvCamera.MV_CC_EnumDevices(MV_GIGE_DEVICE | MV_USB_DEVICE, self.st_device_list)
        if ret != 0:
            strError = "Enum devices fail! ret = :" + ToHexStr(ret)
            print(strError)
            return 0
        if self.st_device_list.nDeviceNum == 0:
            print("Find no device")
            return 0
        print("Find %d devices!" % self.st_device_list.nDeviceNum)
        self.isFindCam = True
        return self.st_device_list.nDeviceNum

    # 打开相机
    def open_device(self):
        self.isOpen = False
        self.obj_cam = MvCamera()
        self.obj_cam_operation = CameraOperation(self.obj_cam, self.st_device_list, self.n_connect_num)
        ret = self.obj_cam_operation.Open_device()
        if 0 != ret:
            strError = "Open device failed ret:" + ToHexStr(ret)
            print(strError)
            self.isOpen = False
        else:
            self.obj_cam_operation.Set_trigger_mode(False)
            self.obj_cam_operation.Get_parameter()
            print(self.obj_cam_operation.frame_rate)
            print(self.obj_cam_operation.gain)
            print(self.obj_cam_operation.exposure_time)
            self.isOpen = True
        return ret

    # 开始拉流
    def start_grabbing(self, q_image):
        ret = -1
        self.isGrabbing = False
        if self.obj_cam_operation == None:
            return ret
        ret = self.obj_cam_operation.Start_grabbing(q_image)
        if ret != 0:
            strError = "Start grabbing failed ret:" + ToHexStr(ret)
            print(strError)
        else:
            self.isGrabbing = True
        return ret
     # 关闭相机
    def close_device(self):
        if self.obj_cam_operation == None:
            return
        if self.isOpen:
            self.obj_cam_operation.Close_device()
            self.isOpen = False
        self.isGrabbing = False

    # 停止拉流
    def stop_grabbing(self):
        ret = -1
        if self.obj_cam_operation == None:
            return ret
        if self.isGrabbing:
            ret = self.obj_cam_operation.Stop_grabbing()
            if ret != 0:
                strError = "Stop grabbing failed ret:" + ToHexStr(ret)
                print(strError)
            self.isGrabbing = False
        return ret

    # 获取参数
    def Get_CamParameter(self):
        ret = -1
        if self.obj_cam_operation == None:
            return ret
        ret = self.obj_cam_operation.Get_parameter()
        if ret != MV_OK:
            strError = "Get param failed ret:" + ToHexStr(ret)
            print(strError)
        return ret

    # 设置参数
    def Set_CamParameter(self, frame_rate, exposure_time, gain):
        ret = -1
        if self.obj_cam_operation == None:
            return ret
        self.frame_rate = frame_rate
        self.exposure_time = exposure_time
        self.gain = gain
        ret = self.obj_cam_operation.Set_parameter(self.frame_rate, self.exposure_time, self.gain)
        if ret != MV_OK:
            strError = "Set param failed ret:" + ToHexStr(ret)
            print(strError)
        return ret

    # 设置触发模式
    def Set_CamMode(self, is_trigger_mode):
        """
        :param is_trigger_mode: False continue mode   True trigger mode
        :return:
        """
        ret = -1
        if self.obj_cam_operation == None:
            return ret
        ret = self.obj_cam_operation.Set_trigger_mode(is_trigger_mode)
        if ret != 0:
            strError = "Set trigger mode failed ret:" + ToHexStr(ret)
            print(strError)
        self.isCamMode = is_trigger_mode
        return ret

    # 触发模式下触发一次
    def Set_Trigger_once(self):
        ret = -1
        if self.obj_cam_operation == None:
            return ret
        ret = self.obj_cam_operation.Trigger_once()
        if ret != 0:
            strError = "TriggerSoftware failed ret:" + ToHexStr(ret)
            print(strError)
        return ret





